package cn.bluethink.eguan.core.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.bluethink.eguan.core.security.Token;
import cn.bluethink.eguan.core.service.EgMemberService;
import cn.bluethink.eguan.core.service.EgStaffService;
import cn.bluethink.eguan.core.service.EgTavernService;
import cn.bluethink.eguan.core.service.EgUserService;
import cn.bluethink.eguan.filter.NameFilter;
import cn.bluethink.eguan.model.core.EgOType;
import cn.bluethink.eguan.model.core.EgObject;
import cn.bluethink.eguan.model.core.EgPurview;
import cn.bluethink.eguan.model.core.EgRole;
import cn.bluethink.eguan.model.core.EgUser;
import cn.bluethink.eguan.result.PageInfo;
import cn.bluethink.eguan.result.ResponseMessage;
import cn.bluethink.eguan.result.ResponseResult;

/**
 * eguan+门派成员控制器
 * 
 * @title 苏州中科蓝迪公司所有(c)2016-2021
 * @version 0.1.0
 * @describe
 * @author liuxn
 * @date 2018年10月16日
 * @note 修订历史： 1.liuxn于2018/10/16日构建初始版本0.1 2.wanglei于2018/10/17日实现接口功能
 *       3.wanglei于2018/11/8修订查询接口实现方式为拼接sql
 */
@CrossOrigin
@RestController
@RequestMapping("/eguan/v1.0/member")
public class EgMemberController {

    @Autowired
    private EgMemberService memberService;

    @Autowired
    private EgUserService userService;

    @Autowired
    private EgTavernService tavernService;

    @Autowired
    private EgStaffService staffService;

    /**
     * 根据条件查询门派成员列表，包括成员角色信息，账户信息，经验值，技能值
     * 
     * @param filter
     * @return
     * @throws Exception
     */
    @Token
    @PostMapping("/query")
    public ResponseResult<PageInfo<EgUser>> query(@RequestBody NameFilter filter) throws Exception {
        return new ResponseResult<>(ResponseMessage.OK, memberService.query(filter, true));
    }

    /**
     * 根据门派ID和用户Id移除门派成员
     * 
     * @param fid
     * @param uid
     * @return
     * @throws Exception
     */
    @Token(purviews = { EgPurview.MEMBER_MG, EgPurview.OBJECT_CHECK })
    @PostMapping("/remove")
    @Transactional(transactionManager = "eguanTransactionManager", rollbackFor = Exception.class)
    public ResponseResult<Long> remove(@RequestParam Long fid, @RequestParam Long uid) throws Exception {
        EgUser user = userService.detail(uid);
        if (user == null) {
            return new ResponseResult<>(ResponseMessage.DATA_NOT_FOUND.getStatus(), "系统中不存在改用户，请联系系统管理员！");
        }
        if (staffService.bShopkeeper(fid, uid)) {
            return new ResponseResult<>(ResponseMessage.DELETE_FAIL.getStatus(), "该成员为门派中驿馆掌柜，请移交掌柜后重试！");
        }
        // 删除门派成员
        if (!memberService.remove(fid, uid)) {
            return new ResponseResult<>(ResponseMessage.DELETE_FAIL);
        }
        // 判断该门派中是否有驿馆
        List<Long> tavernIds = tavernService.egidsByFid(fid);
        // 如果该门派中有驿馆，并且该用户存在门派中的驿馆时，将驿馆中的对应伙计删除
        if (tavernIds != null && tavernIds.size() > 0) {
            if (staffService.removeStaffByEg(tavernIds, uid) == 0L) {
                return new ResponseResult<>(ResponseMessage.DELETE_FAIL.getStatus(), "移除门派成员失败，请重试！");
            }
        }
        // 获取用户所在的位置
        EgObject pos = user.getPos();
        if (pos == null) {
            return new ResponseResult<>(ResponseMessage.OK, fid);
        }
        Long oid = pos.getId();
        Integer otype = pos.getType().getOtid();
        // 如果当前用户在当前门派中或在当前门派的驿馆中，设置其位置为江湖
        if ((otype.equals(EgOType.FACTION) && oid.equals(fid))
                || (otype.equals(EgOType.TAVERN) && tavernService.existInFaction(fid, oid))) {
            if (userService.updatePos(uid, EgOType.JIANGHU, 0L)) {
                return new ResponseResult<>(ResponseMessage.OK, fid);
            }
            return new ResponseResult<>(ResponseMessage.DELETE_FAIL.getStatus(), "移除门派成员失败，请重试！");
        }
        return new ResponseResult<>(ResponseMessage.OK, fid);
    }

    /**
     * 设置成员角色 (设置为副帮主或门派成员)
     * 
     * @param fid
     * @param uid
     * @param rid 角色ID
     * @return
     * @throws Exception
     */
    @Token(purviews = { EgPurview.APPOINT_VICE_CHEAF })
    @PostMapping("/setrole")
    public ResponseResult<EgUser> setRole(@RequestParam Long fid, @RequestParam Long uid, @RequestParam Integer rid)
            throws Exception {
        if (!rid.equals(EgRole.MEMBER) && !rid.equals(EgRole.VICE_CHEAF)) {
            return new ResponseResult<>(ResponseMessage.PARAMETER_ERROR.getStatus(), "只能是门派成员或副帮主角色标识！");
        }
        EgUser user = memberService.setrole(fid, uid, rid);
        if (user != null) {
            return new ResponseResult<>(ResponseMessage.OK, user);
        }
        return new ResponseResult<>(ResponseMessage.UPDATE_FAIL.getStatus(), "成员角色设置失败！");
    }

}
